package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultList;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.dao.StepBriefDao;
import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.service.DutyService;
import com.fz.us.admin.company.service.PowerService;
import com.fz.us.admin.company.service.StepBriefService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * Service实现类 - 
 * ============================================================================
 * 版权所有 2013
 * ----------------------------------------------------------------------------
 * 
 * @author 
 * 
 * @version 0.1 2011-6-13
 */

@Service
public class StepBriefServiceImpl extends BaseServiceImpl<StepBrief, String> implements StepBriefService {
    @Resource
    private StepBriefDao stepBriefDao;
    @Resource
    private PowerService powerService;
    @Resource
    private DutyService dutyService;

    @Override
    public BaseDao<StepBrief, String> getBaseDao() {
        return stepBriefDao;
    }

    @Override
    public StepBrief getInfoByStepNo(ProcessBrief processBrief, int stepNo) {
        return stepBriefDao.getInfoByStepNo(processBrief, stepNo);
    }

    @Override
    public StepBrief getInfoByCommonConfig(CommonConfig commonConfig) {
        return stepBriefDao.getInfoByCommonConfig(commonConfig);
    }

    @Override
    public List<User> findSpecailByUser(StepBrief stepBrief, Duty duty) {
        List<User> usersList = Lists.newArrayList();
        String strSpecialType = stepBrief.getSpecialType();
        if(strSpecialType != null){
            if(strSpecialType.indexOf("1") != -1){
                Set<Duty> dutySet = powerService.findParentByPower(duty);
                for(Duty duty1 : dutySet){
                    usersList.add(duty1.getUser());
                }
            }
            if(strSpecialType.indexOf("2")!= -1){
                if(duty.getPower()!=null){
                    Duty duty1 = dutyService.getPrincipalDuty(duty.getPower().getDepartment());
                    if(duty1 != null){
                        usersList.add(duty1.getUser());
                    }
                }
            }
            if(strSpecialType.indexOf("3")!= -1){
                if(duty.getPower()!=null){
                    Duty duty1 = dutyService.getParentPrincipal(duty.getPower().getDepartment());
                    if(duty1 != null){
                        usersList.add(duty1.getUser());
                    }
                }
            }
        }
        return usersList ;
    }

    @Override
    public Set<Duty> findSpecialByDuty(StepBrief stepBrief, Duty duty) {
        Set<Duty> dutySet = Sets.newHashSet();
        String strSpecialType = stepBrief.getSpecialType();
        if(strSpecialType != null){
            if(strSpecialType.indexOf("1") != -1){
                dutySet.addAll(powerService.findParentByPower(duty));
            }
            if(strSpecialType.indexOf("2")!= -1){
                if(duty.getPower()!=null){
                    Duty duty1 = dutyService.getPrincipalDuty(duty.getPower().getDepartment());
                    if(duty1 != null){
                        dutySet.add(duty1);
                    }
                }
            }
            if(strSpecialType.indexOf("3")!= -1){
                if(duty.getPower()!=null){
                    Duty duty1 = dutyService.getParentPrincipal(duty.getPower().getDepartment());
                    if(duty1 != null){
                        dutySet.add(duty1);
                    }
                }
            }
        }
        return dutySet;
    }

    @Override
    public SearchResultPaging pagingStepBrief(PagePaging page, List<Rule> rules, Order order, ProcessBrief processBrief, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }
        if(processBrief != null){
            rules.add(RuleHelper.eq("processBrief", processBrief));
        }

        // 默认排序
        if (order == null)
            order = Order.order("stepNo", SearchEnum.OrderType.asc);

        return findPagingBySearch(page, rules, order);

    }

    @Override
    public SearchResultList listStepsBrief(List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("stepNo", SearchEnum.OrderType.asc);

        return findListBySearch(rules, order);
    }
}